//
//  XMMyAddressCell.m
//  XMJJ_zml
//
//  Created by 丁浩 on 2025/5/29.
//

#import "XMMyAddressCell.h"

@interface XMMyAddressCell ()

@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *phoneLabel;
@property (nonatomic, strong) UILabel *addressLabel;

@property (nonatomic, strong) UIButton *setDefaultBtn;

@property (nonatomic, strong) UILabel *stateLabel;

@end

@implementation XMMyAddressCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    [self addSubview:self.nameLabel];
    [self addSubview:self.phoneLabel];
    [self addSubview:self.addressLabel];
    [self addSubview:self.stateLabel];
    _stateLabel.hidden = YES;
    [self.contentView addSubview:self.setDefaultBtn];
    
    UIButton *addressEditBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(13.0) frame:CGRectMake(kScreenWidth - KScaleWidth(5.0) - KScaleWidth(93.0), _setDefaultBtn.y, KScaleWidth(93.0), _setDefaultBtn.height) title:@"编辑地址" titleColor:kColorHEX(0xB8B8B8) image:@"mine_address_edit_icon"];
    [addressEditBtn addTarget:self action:@selector(addressEditAction:) forControlEvents:UIControlEventTouchUpInside];
    [addressEditBtn layoutButtonWithEdgeInsetsStyle:CSButtonEdgeInsetsStyleLeft ImageTitleSpace:KScaleWidth(5.0)];
    [self.contentView addSubview:addressEditBtn];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(KScaleWidth(12.0), _addressLabel.maxY + KScaleWidth(5.0), kScreenWidth - KScaleWidth(12.0)*2, 1)];
    lineView.backgroundColor = kColorHEX(0xF1F1F1);
    [self addSubview:lineView];
}

- (void)setContentWithAddressModel:(XMAddressModel *)addressModel {
    _nameLabel.text = addressModel.name;//@"张小白";
    _phoneLabel.text = addressModel.mobile;//@"181****0202";
    _addressLabel.text = [NSString stringWithFormat:@"%@ %@", addressModel.areaName, addressModel.detailAddress];//@"保定市莲池区东风中路268号110-11";
    _setDefaultBtn.enabled = !addressModel.defaultStatus;
    UIView *pointView = [_setDefaultBtn viewWithTag:32];
    pointView.backgroundColor = addressModel.defaultStatus ? kColorHEX(0xFFD800) : kColorHEX(0xB8B8B8);
}

//设为默认地址
- (void)setDefaultAction:(UIButton *)button {
    self.setDefaultBlock ? self.setDefaultBlock() : nil;
}

//编辑
- (void)addressEditAction:(UIButton *)button {
    self.editBlock ? self.editBlock() : nil;
}

#pragma mark - Init
- (UILabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(15.0), KScaleWidth(3.0), KScaleWidth(55.0), KScaleWidth(32.0)) font:KFontSize(16.0) textColor:kColorHEX(0x333333)];
    }
    return _nameLabel;
}

- (UILabel *)phoneLabel {
    if (!_phoneLabel) {
        _phoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(70.0), self.nameLabel.y, kScreenWidth/2, _nameLabel.height) font:KFontSize(16.0) textColor:kColorHEX(0x333333)];
    }
    return _phoneLabel;
}

- (UILabel *)addressLabel {
    if (!_addressLabel) {
        _addressLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(15.0), self.nameLabel.maxY, kScreenWidth - KScaleWidth(15.0)*2, KScaleWidth(28.0))];
    }
    return _addressLabel;
}

- (UILabel *)stateLabel {
    if (!_stateLabel) {
        _stateLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(15.0), self.nameLabel.maxY + KScaleWidth(5.0), KScaleWidth(38.0), KScaleWidth(18.0)) font:KFontSize(10.0) textColor:kColorHEX(0xffffff) textAlignment:NSTextAlignmentCenter];
    }
    return _stateLabel;
}

- (UIButton *)setDefaultBtn {
    if (!_setDefaultBtn) {
        _setDefaultBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _setDefaultBtn.frame = CGRectMake(KScaleWidth(10.0), self.addressLabel.maxY + KScaleWidth(12.0), KScaleWidth(95.0), KScaleWidth(28.0));
        [_setDefaultBtn setTitle:@"设为默认" forState:UIControlStateNormal];
        [_setDefaultBtn setTitleColor:kColorHEX(0xB8B8B8) forState:UIControlStateNormal];
        [_setDefaultBtn setTitle:@"默认地址" forState:UIControlStateDisabled];
        [_setDefaultBtn setTitleColor:kColorHEX(0x333333) forState:UIControlStateDisabled];
        [_setDefaultBtn addTarget:self action:@selector(setDefaultAction:) forControlEvents:UIControlEventTouchUpInside];
        [_setDefaultBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, KScaleWidth(15.0), 0, -KScaleWidth(15.0))];
        
        UIView *pointView = [[UIView alloc] initWithFrame:CGRectMake(KScaleWidth(7.0), (_setDefaultBtn.height - KScaleWidth(16.0))/2, KScaleWidth(16.0), KScaleWidth(16.0))];
        pointView.backgroundColor = kColorHEX(0xB8B8B8);//kColorHEX(0xFFD800);
        pointView.layer.cornerRadius = pointView.height/2;
        pointView.tag = 32;
        [_setDefaultBtn addSubview:pointView];
    }
    return _setDefaultBtn;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
